package com.framework.plm.bas.exception;

import org.apache.commons.lang.StringUtils;

import com.framework.plm.bas.util.UtilMessage;

public class CheckedException extends Exception {

	private static final long serialVersionUID = 1L;

	private String errorCode = null;
	private Object[] errorParam = null;
	private Throwable caseObj = null;

	public CheckedException(String errorCode) {
		this.errorCode = errorCode;
	}

	public CheckedException(String errorCode, Object[] errorParam) {
		this.errorCode = errorCode;
		this.errorParam = errorParam;
	}

	public CheckedException(String errorCode, Object[] errorParam, Throwable t) {
		super(t);
		this.errorCode = errorCode;
		this.errorParam = errorParam;
		this.caseObj = t;
	}

	public CheckedException(String message, Throwable t) {
		super(message, t);
	}

	public String getErrorCode() {
		return errorCode;
	}

	public Object[] getErrorParam() {
		return errorParam;
	}
	
	public Throwable getCaseObj() {
		return caseObj;
	}

	public String getMessage4ErrorCode() {
		String result = null;
		if (getErrorCode() != null && getErrorCode().trim().length() > 0) {
			if(getCaseObj()==null){
				result = UtilMessage.getMessage(getErrorCode(), getErrorParam());
			}else{
				Throwable errors = getCaseObj();
				while(errors!=null){
					if(StringUtils.isBlank(errors.getMessage())){
						errors = errors.getCause();
					}else{
						break;
					}
				}
				if (errors!=null){
					result = UtilMessage.getMessage(getErrorCode(), getErrorParam(), null, errors.getClass());
				}else{
					result = UtilMessage.getMessage(getErrorCode(), getErrorParam());
				}
			}
		} else {
			result = super.getMessage();
		}
		return result;
	}

	public String getMessage() {
		if(getErrorCode()!=null){
			return getErrorCode();//local
		}else{
			return super.getMessage();
		}
	}
	
}